<template>
    <div>
        <div class="wrap">
            <bread-crumb></bread-crumb>
            <h1>不带头像的学生的信息展示</h1>
            <el-table id="work-table" :data="tableData" style="width: 100%" border height="600px">
            <el-table-column prop="stu_id" label="学号" width="120">
            </el-table-column>
            <el-table-column prop="stu_name" label="姓名" width="120">
            </el-table-column>
            <el-table-column prop="stu_sex" label="性别" width="120">
            </el-table-column>
            <el-table-column prop="stu_jg" label="籍贯" width="160">
            </el-table-column>
            <el-table-column prop="stu_date" label="出生日期" width="180">
                <template slot-scope="scope">
                    <p>{{ $moment(scope.row.stu_date).format("YYYY-MM-DD") }}</p>
                </template>
            </el-table-column>
            <el-table-column prop="stu_gbf" label="男女朋友" width="120">
            </el-table-column>
            <el-table-column prop="stu_hobby" label="爱好及自我评价" width="450">
            </el-table-column>
        </el-table>
            <el-button @click="exportExcel">将数据导出到本地的Excel当中</el-button>
        </div>
    </div>
</template>

<script>
import { req_stu_info_nohead } from "@/api/stuInfo.js";
//导出Excel使用两个包 xlsx@^0.16.0   file-saver@^2.0.2
import XLSX from "xlsx";
import FileSaver from "file-saver";
export default {
    async mounted() {
        let result = await req_stu_info_nohead();
        console.log(result);
        this.tableData = result.data;
    },
    data() {
        return {
            tableData: [],
        };
    },
    methods: {
        exportExcel() {
            console.log("导出到Excel中");
            let wb = XLSX.utils.table_to_book(document.getElementById("work-table"), {
                raw: true,
            });

            // 将工作簿当中的内容变为二进制 输出
            let wbout = XLSX.write(wb, {
                bookType: "xlsx",
                bookSST: true,
                type: "array",
            });
            // 将得到的二进制文件存储到Blob
            try {
                FileSaver.saveAs(
                    new Blob([wbout], { type: "application/octet-stream" }),
                    "stuInfo.xlsx"
                );
            } catch (e) {
                console.log(e);
            }
        },
    },
};
</script>

<style lang="scss" scoped>
div {
    .wrap {
        height: 30px;
    }
}
</style>